﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace GuestList.ViewModels
{
    public delegate bool Predicate();

    public class RelayCmd : ICommand
    {
        Predicate _canExecute;
        Action _execute;

        protected RelayCmd(Predicate canExecute)
        {
            _canExecute = canExecute;
        }

        public RelayCmd(Action execute, Predicate canExecute)
        {
            _execute = execute;
            _canExecute = canExecute;
        }

        #region ICommand Members

        public bool CanExecute(object parameter)
        {
            return _canExecute.Invoke();
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public virtual void Execute(object parameter)
        {
            _execute.Invoke();
        }

        #endregion
    }

    public class RelayCmd<T> : RelayCmd
    {
        Action<T> _execute;

        public RelayCmd(Action<T> execute, Predicate canExecute) : base(canExecute)
        {
            _execute = execute;
        }

        public override void Execute(object parameter)
        {
            _execute.Invoke((T)parameter);
        }
    }
}
